/*
 * Copyright (c) 2012,东方口岸科技有限公司
 * All rights reserved.
 * 文件名称：ZdCalculate.java
 * 摘    要：
 * 版本：1.0
 * 作    者：fengzhiyong
 */
package com.cneport.transfer.router.biz;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.cneport.transfer.constant.RouterTypeConstants;
import com.cneport.transfer.constant.StatusConstants;
import com.cneport.transfer.router.api.IRouterMessageCalculator;
import com.cneport.transfer.router.model.NodeRelation;
import com.cneport.transfer.router.model.Router;
import com.cneport.transfer.router.support.service.SequenceService;
import com.cneport.transfer.util.DateUtil;

public class RouterMessageShortCalculator implements IRouterMessageCalculator {

    private static final Log logger = LogFactory
    .getLog(RouterMessageShortCalculator.class);
    @Autowired
    private  SequenceService sequenceService;
    private String routeEffectTime;
    private int routeEffcetDelayDays;
    private Date routeEffectOneDay;

    public String getRouteEffectTime() {
        return routeEffectTime;
    }

    public void setRouteEffectTime(String routeEffectTime) {
        this.routeEffectTime = routeEffectTime;
    }

    public int getRouteEffcetDelayDays() {
        return routeEffcetDelayDays;
    }

    public void setRouteEffcetDelayDays(int routeEffcetDelayDays) {
        this.routeEffcetDelayDays = routeEffcetDelayDays;
    }

    public Date getRouteEffectOneDay() {
        return routeEffectOneDay;
    }

    public void setRouteEffectOneDay(Date routeEffectOneDay) {
        this.routeEffectOneDay = routeEffectOneDay;
    }

    public List<Router> calculateRouteInfo(List<NodeRelation> nodeRelInfoList,
            String version) {
        if (nodeRelInfoList == null || nodeRelInfoList.size() == 0)
            return null;
        List<NodeRelation> linkInfoList = new ArrayList<NodeRelation>();
        List<String> nodes = new ArrayList<String>();

        for (NodeRelation nodeRelInfo : nodeRelInfoList) {
            if (StatusConstants.STATUS_OFF.equals(nodeRelInfo.getStatus())
                    || nodeRelInfo.getWeight() == null
                    || nodeRelInfo.getWeight() < 0) {
                continue;
            }
            NodeRelation nodeLinkInfo = new NodeRelation();
            nodeLinkInfo.setScrNodeId(nodeRelInfo.getScrNodeId());
            nodeLinkInfo.setDestNodeId(nodeRelInfo.getDestNodeId());
            nodeLinkInfo.setWeight(nodeRelInfo.getWeight());
            linkInfoList.add(nodeLinkInfo);
            if (!nodes.contains(nodeRelInfo.getScrNodeId())) {
                nodes.add(nodeRelInfo.getScrNodeId());
            }
        }
        String nextNode = null;
        Date now = new Date();
        if (StringUtils.isEmpty(version)) {
            version = DateUtil.convertDateToYYYYMMddHHmmssSSS(now);
        }

        Date effectTime = new Date();
        Date creatTime = new Date();

        if (routeEffcetDelayDays != 0) {
            effectTime = DateUtil.addDataDaysCleanLow(effectTime,
                    routeEffcetDelayDays);
        }

        if (StringUtils.isNotEmpty(routeEffectTime)
                && routeEffectOneDay != null) {
            effectTime.setHours(routeEffectOneDay.getHours());
            effectTime.setMinutes(routeEffectOneDay.getMinutes());
            effectTime.setSeconds(routeEffectOneDay.getSeconds());
        }

        List<Router> routeInfoList = new ArrayList<Router>();
 
        for (String startPort : nodes) {
            for (String destPort : nodes) {
                if (destPort.equals(startPort))
                    continue;
                NextJumpFinder findNextRout = new NextJumpFinder();
                nextNode = findNextRout.getNextJump(linkInfoList, startPort,
                        destPort);
                if (StringUtils.isEmpty(nextNode))
                    continue;

                Router routeInfo = new Router();
                routeInfo.setRouterId(sequenceService.getSequence("routerID"));
                logger.debug("RouterID="+routeInfo.getRouterId());
                routeInfo.setRouterVer(version);
                routeInfo.setStartNode(startPort);
                routeInfo.setEndNode(destPort);
                routeInfo.setRouterExpression(nextNode);
                routeInfo.setRouterEffectTime(effectTime);
                routeInfo.setRouterType(RouterTypeConstants.DTF_ROUTE);
                routeInfoList.add(routeInfo);
            }
        }

        return routeInfoList;
    }
}
